<?php
include('admin_advert.php');
/**
 * Created by PhpStorm.
 * User: tsa
 * Date: 17/12/2013
 * Time: 11:34 πμ
 */
if ($_GET['id_ad'] == "0")
{
    if ($_POST['click'] == "1")
    {
        $cc1 = intval($_POST['cc']);
        $i=0;
        while ($cc1>$i)
        {
            $i=$i+1;
            $ccategory_id = 'category_id'.$i;
            $iid = 'fffid'.$i;
            $oordering = 'ordering'.$i;
            $sql_qq = 'SELECT * FROM category_matrix WHERE category_id='.intval($_POST[$ccategory_id]).' LIMIT 1';
            $result_qq = $result = mysqli_query($con, $sql_qq);
            $row_qq = mysqli_fetch_array($result_qq);
            $found = 0;
            if (intval($_POST[$ccategory_id]) != "")
            {
                // echo $sql_qq.'<br>';
                if (empty($row_qq)){
                    $found_f =0;
                }
                else
                {
                    $found_f = 1;
                }
                if ($found_f == 1)
                {
                    //    echo $oordering.'='.$_POST[$oordering].'<br>';
                    $sql_q = 'UPDATE category_matrix SET category_id='.intval($_POST[$ccategory_id]).',parent_id='.intval($_POST['parent_id']).',ordering='.intval($_POST[$oordering]).' WHERE id='.intval($row_qq['id']);
                    $result = mysqli_query($con, $sql_q);
                    //echo $sql_q.'<br>';
                }
                else
                {
                    $sql_q = 'INSERT INTO category_matrix (`id`,`category_id`,`parent_id`,`ordering`) VALUES (NULL,'.intval($_POST[$ccategory_id]).','.intval($_POST['parent_id']).','.intval($_POST[$oordering]).')';
                    //echo $sql_q.'<br>';
                    $result = mysqli_query($con, $sql_q);
                }
            }
            else
            {
                $sql_q = 'DELETE FROM category_matrix WHERE parent_id='.intval($_POST['parent_id']).' AND category_id='.intval($_POST[$iid]);
                // echo $sql_q.'<br>';
                $result = mysqli_query($con, $sql_q);
            }
        }
    }

    $click = "0";
    $_POST["click"] = "0";
    ?>
    <script>
        function my_yoz(){
            document.form_category_matrix_insert.click.value='0';
            document.getElementById("form_category_matrix_insert").submit();
        }
    </script>
    <?php
    $form = '<h4>Αντιστοίχηση κατηγοριών</h4>';
    $form = $form . '<form name="form_category_matrix_insert" id="form_category_matrix_insert" method="post">';
    $form = $form . '<label>Γονική κατηρογία</label>';
    $form = $form . '<select name="parent_id" onchange="my_yoz()"/>';
    $form = $form . '<option selected value="">Επιλέξτε</option>';
    $sql_1 = "SELECT category.id,category.name,category.divname FROM category group by name";
    $result1 = mysqli_query($con, $sql_1);
// echo $sql_1;
    $_POST["click"] = "0";
    // echo $_POST["click"];
    while($row = mysqli_fetch_array($result1))
    {
        if (intval($_POST['parent_id']) == $row['id'])
        {
            $form = $form . '<option selected value="'.$row['id'].'">'.$row['name'].'</option>';
        }
        else{
            $form = $form . '<option value="'.$row['id'].'">'.$row['name'].'</option>';
        }
    }
    $form = $form . '</select>';
    if (!empty($_POST['parent_id'])){
        $form = $form . '<label>Κατηγορίες</label>';
        $sql_ = "SELECT category.id,category.name FROM category group by name";
        $result2 = mysqli_query($con, $sql_);
        $cc=0;
        while($row = mysqli_fetch_array($result2))
        {
            $sql_3 = 'SELECT category_matrix.id as fid, category_matrix.category_id as fmfid, category_matrix.parent_id as fmpi, category_matrix.ordering as fmo FROM category_matrix where category_matrix.category_id='.intval($row['id']).' AND category_matrix.parent_id='.intval($_POST['parent_id']);
            //echo $sql_3.'<br>';
            $result_3 = mysqli_query($con, $sql_3);
            $row3 = mysqli_fetch_array($result_3);
            $cc=$cc+1;
            if (empty($row3))
            {
                $form = $form . '<input type="checkbox" name ="category_id'.$cc.'" value="'.$row['id'].'" /> '.$row['name'].' Ταξινόμηση ';
                $form = $form . '<input type="text" name="ordering'.$cc.'" value="" class="input-mini"/><br>';
                $form = $form . '<input name="id'.$cc.'" type="hidden" value=""/>';
                $form = $form . '<input name="fffid'.$cc.'" type="hidden" value="'.$row['id'].'"/>';
            }
            else
            {
                $form = $form . '<input type="checkbox" name ="category_id'.$cc.'" value="'.$row['id'].'" checked/> '.$row['name'].' Ταξινόμηση ';
                $form = $form . '<input type="text" name="ordering'.$cc.'" value="'.$row3['fmo'].'" class="input-mini"/><br>';
                $form = $form . '<input name="id'.$cc.'" type="hidden" value="'.$row3['fid'].'"/>';
                $form = $form . '<input name="fffid'.$cc.'" type="hidden" value="'.$row['id'].'"/>';
            }
        }
        $form = $form . '<input name="cc" type="hidden" value="'.$cc.'"/>';
        $form = $form . '<input name="click" type="hidden" value="1"/>';
        $form = $form . '<input type="submit" value="Αντιστοίχιση"/>';
        $form = $form . '</form>';

    }
    echo $form;
}
elseif ($_GET['id_ad'] == "1") {
$sql_2 = "SELECT category.id AS aid, category.name AS aname, category_matrix.id as bid, category_matrix.category_id AS bcid, category_matrix.parent_id AS bparent_id, category_matrix.ordering AS bordering FROM  category JOIN category_matrix WHERE category.id = category_matrix.category_id GROUP BY category_matrix.id";
$result = mysqli_query($con, $sql_2);
echo '<table border="1">';
echo '<tr><td>Id</td><td>Category id</td><td>Name</td><td>Parent_id</td><td>Ordering</td></tr>';
while($row = mysqli_fetch_array($result))
{
    echo '<tr><td>'.$row['bid'].'</td><td>'.$row['bcid'].'</td><td>'.$row['aname'].'</td><td>'.$row['bparent_id'].'</td><td>'.$row['bordering'].'</td><td>[ <a href="?page=admin_advert/category_matrix_edit&id_ad=2&id='.$row['bid'].'">Επεξεργασία</a> ]</td><td>[ <a href="?page=admin_advert/category_matrix_edit&id_ad=3&id='.$row['bid'].'">Διαγραφή</a> ]</td></tr>';
}
echo '</table>';
}
else
{
    echo "<br><h2>PAGE NOT FOUND</h2>";
}
?>